#-*- coding:utf-8 -*-
import os
import sys
from PySide2.QtGui import *
from PySide2.QtCore import *
from PySide2.QtWidgets import *
import webbrowser

class AddReference(QWidget):
    reference_Signal=Signal(tuple)
    def __init__(self):
        super(AddReference,self).__init__()

        # self.current_reference_path=reference_path
        self.setWindowTitle('添加参考/教程链接')

        self.mainLayout=QVBoxLayout()
        self.url_label=QLabel('路径地址:')
        self.url_edit=QLineEdit()
        self.remark_label=QLabel('备注说明')
        self.remark_text=QTextEdit()
        self.btn_layout=QHBoxLayout()
        self.OK_btn=QPushButton('添加')
        self.OK_btn.clicked.connect(self.__OKbtn)
        self.No_btn=QPushButton('取消')
        self.No_btn.clicked.connect(self.__closeWindow)
        self.btn_layout.addWidget(self.OK_btn)
        self.btn_layout.addWidget(self.No_btn)
        self.mainLayout.addWidget(self.url_label)
        self.mainLayout.addWidget(self.url_edit)
        self.mainLayout.addWidget(self.remark_label)
        self.mainLayout.addWidget(self.remark_text)
        self.mainLayout.addLayout(self.btn_layout)

        self.setLayout(self.mainLayout)


    def __OKbtn(self):
        if not self.url_edit.text() or not self.remark_text.toPlainText():
            QMessageBox.information(self,'注意','请输入完整链接与备注')

        else:

            url=self.url_edit.text()
            remark=self.remark_text.toPlainText()
            temp_tuple=(remark,url)
            self.reference_Signal.emit(temp_tuple)
            # QMessageBox.information(self,'提示','OK ')
            print(temp_tuple)
            self.close()
    def __closeWindow(self):
        self.close()



class ShotInfo(QWidget):

    def __init__(self):
        super(ShotInfo,self).__init__()

        #全局变量
        # self.all_reference_list=[]#当前所有的链接列表
        self.current_reference_path=''
        self.current_real_shot_path = ''

        self.mainLayout=QVBoxLayout()
        self.text=QTextBrowser()

        self.reference_layout=QHBoxLayout()
        self.reference_label=QLabel('参考/教程链接:')
        self.reference_btn=QPushButton('添加参考')
        self.reference_btn.clicked.connect(self.__openReferenceWindow)
        self.reference_layout.addWidget(self.reference_label)
        self.reference_layout.addWidget(self.reference_btn)
        self.reference_list=QListWidget()
        self.reference_list.doubleClicked.connect(self.__goReference)
        self.reference_list.setContextMenuPolicy(Qt.CustomContextMenu)
        self.reference_list.customContextMenuRequested.connect(self.__rightMenu)

        self.mainLayout.addWidget(self.text)
        self.mainLayout.addLayout(self.reference_layout)
        self.mainLayout.addWidget(self.reference_list)
        self.setLayout(self.mainLayout)

        # self.__test()
    def setCurrentReferencePath(self,current_shot_path,current_shot_name):
        '''
        设置当前参考链接目录
        :param current_shot_path:
        :return:
        '''
        self.current_real_shot_path = current_shot_path
        self.current_shot_name=current_shot_name
        if current_shot_path!='':
            self.current_reference_path=os.path.join(current_shot_path,'reference')


    def __rightMenu(self):
        menu=QMenu()
        delete_action=QAction('删除')
        delete_action.triggered.connect(self.__deleteReference)
        menu.addAction(delete_action)
        menu.exec_(QCursor.pos())

    def showInfo(self):
        '''
        :return:
        '''
        self.text.clear()
        self.reference_list.clear()
        if self.current_real_shot_path:
            info_path=os.path.join(self.current_real_shot_path,'info')
            info=''#{'artist': 'Master', 'given_time': '1', 'status': 1, 'price': '100', 'requirement': '爆炸','start_day':'2019-07-12'}
            with open(info_path,'r',encoding='utf-8') as r:
                read=r.read()
                info=eval(read)
            status=['制作中','通过','返修','暂停']

            text='镜头名称:{}\n制 作 者:{}\n分配时间:{}天\n审核状态:{}\n制作价格:{}元\n制作内容:{}\n开始时间:{}'.format(
                self.current_shot_name,info['artist'],info['given_time'],status[info['status']],info['price'],info['requirement'],info['start_day'])
            self.text.setText(text)


            index_list=os.listdir(self.current_reference_path)
            if index_list:
                for i in index_list:
                    re_tuple=''
                    index_path=os.path.join(self.current_reference_path,i)
                    with open(index_path,'r',encoding='utf-8') as r:
                        read=r.read()
                        re_tuple=eval(read)

                    self.__addReference(re_tuple)



    def __openReferenceWindow(self):
        self.reference_window=AddReference()
        self.reference_window.reference_Signal.connect(self.__addReference)
        self.reference_window.show()


    def setReference(self,re_list):

        self.reference_list.addItems(re_list)

    def __addReference(self,re_tuple):
        '''
        添加参考
        :param re_tuple:
        :return:
        '''
        remark=re_tuple[0]
        url=re_tuple[1]
        item=QListWidgetItem()
        item.setText(remark)
        item.setData(1000,url)
        count=self.reference_list.count()
        self.__saveReference(count,re_tuple)
        self.reference_list.addItem(item)
        # self.all_reference_list.append(remark)

    def __deleteReference(self):

        row=self.reference_list.currentIndex().row()
        index_path=os.path.join(self.current_reference_path,str(row))
        os.remove(index_path)
        self.reference_list.takeItem(row)

    def __goReference(self):
        '''
        打开链接
        :return:
        '''
        item=self.reference_list.currentItem()
        data=item.data(1000)
        url=data

        if url.startswith('http'):
            webbrowser.open(url)
        else:
            os.startfile(url)
        os.startfile(url)

    def __saveReference(self,index,re_tuple):
        '''
        保存链接
        :param index:
        :return:
        '''
        if self.current_reference_path !='':
            index_path=os.path.join(self.current_reference_path,str(index))
            with open(index_path,'w',encoding='utf-8') as w:
                w.write(str(re_tuple))

    def __test(self):
        text='镜头名称:{}\n制 作 者:{}\n分配时间:{}天\n审核状态:{}\n制作价格:{}元\n制作内容:{}'.format('aa','Master','1','通过','120','子弹')
        self.text.setText(text)
        re_list=['E:\剑心真人','https://www.baidu.com/']

        self.setReference(re_list)

if __name__ == '__main__':
    app=QApplication(sys.argv)
    s=ShotInfo()
    s.show()
    # a=AddReference()
    # a.show()
    sys.exit(app.exec_())